package com.matimdev.scene;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.SpriteMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;

import com.matimdev.base.BaseScene;
import com.matimdev.manager.SceneManager;
import com.matimdev.manager.SceneManager.SceneType;

public class OptionsScene extends BaseScene
{
	//---------------------------------------------
	// VARIABLES
	//---------------------------------------------
	
	private MenuScene optionsChildScene;
	private Text sonidoText;	
	
	
	//---------------------------------------------
	// METHODS FROM SUPERCLASS
	//---------------------------------------------

	@Override
	public void createScene()
	{
		createBackground();
		createOptionsChildScene();
	}

	@Override
	public void onBackKeyPressed()
	{
		SceneManager.getInstance().setScene(SceneType.SCENE_MENU);
	}

	@Override
	public SceneType getSceneType()
	{
		return SceneType.SCENE_OPTIONS;
	}
	

	@Override
	public void disposeScene()
	{
		// TODO Auto-generated method stub
	}
	
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem, float pMenuItemLocalX, float pMenuItemLocalY)
	{
		return mChildrenIgnoreUpdate;
		
	}
	
	//---------------------------------------------
	// CLASS LOGIC
	//---------------------------------------------
	
	private void createBackground()
	{
		attachChild(new Sprite(400, 240, resourcesManager.options_background_region, vbom)
		{
    		@Override
            protected void preDraw(GLState pGLState, Camera pCamera) 
    		{
                super.preDraw(pGLState, pCamera);
                pGLState.enableDither();
            }
		});
	}
	
	private void createOptionsChildScene()
	{
		optionsChildScene = new MenuScene(camera);
		optionsChildScene.setBackgroundEnabled(false);
		sonidoText = new Text(0, 0, resourcesManager.font, "Sonido", new TextOptions(HorizontalAlign.LEFT), vbom);
		sonidoText.setPosition(camera.getCenterX(), camera.getCenterY());
		optionsChildScene.attachChild(sonidoText);
		
		optionsChildScene.setTouchAreaBindingOnActionDownEnabled(true);
		
		
		/* Create the music mute/unmute button */
		ButtonSprite mMuteButton = new ButtonSprite(camera.getCenterX()+150, camera.getCenterY(), resourcesManager.texturaSonido, vbom) {

		  /* Override the onAreaTouched() method allowing us to define custom
		  * actions */
			@Override
			 public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				      float pTouchAreaLocalX, float pTouchAreaLocalY) {
				    /* In the event the mute button is pressed down on... */
				    if (pSceneTouchEvent.isActionDown()) {
				      if (resourcesManager.music.isPlaying()) {
				        /*  If music is playing, pause it and set tile index to  MUTE  */
				        this.setCurrentTileIndex(1);
				        resourcesManager.music.pause();
				      } else {
				        /* If music is paused, play it and set tile index to UNMUTE */
				    	  
				    	  this.setCurrentTileIndex(0);
				    	  resourcesManager.music.play();
				        
				      }
				      return true;
				    }
				    return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
				        pTouchAreaLocalY);
				  }
			

		};

		resourcesManager.music.setLooping(true);
		/* Set the current tile index to unmuted on application startup */

		/* Register and attach the mMuteButton to the Scene */
		optionsChildScene.registerTouchArea(mMuteButton);
		optionsChildScene.attachChild(mMuteButton);

		
		
		
		
		
		
		setChildScene(optionsChildScene);
	}

}
